package edu.unlv.cs.ladders.entities;

import java.util.HashSet;
import java.util.Set;
import edu.unlv.cs.ladders.entities.BaseEntity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.entity.RooEntity;
import javax.validation.constraints.NotNull;

@Entity
@RooJavaBean
@RooToString
@RooEntity(finders = { "findTagsByName" })
public class Tag extends BaseEntity implements Comparable<Tag> {

	private static final long serialVersionUID = 1L;

	@ManyToMany
    private Set<Tournament> tournaments;

    @NotNull
    @Column(unique=true)
    private String name;
    
    // used in compare to
    private Integer priority = 0;
    
    public String toString(){
    	return name;
    }

	public int compareTo(Tag other) {
		if(this.priority < other.priority){
			return -1;
		} else if(this.priority > other.priority){
			return 1;
		} else {
			return 0;
		}
	}

	public void addTournament(Tournament tournament) {
		if(!getTournaments().contains(tournament)){
			getTournaments().add(tournament);
			merge();
		}
		
	}
	public Set<Tournament> getTournaments(){
		if(this.tournaments==null){
			this.tournaments = new HashSet<Tournament>();
		}
		return this.tournaments;
	}
}
